# -*- Python -*-
#
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Configuration file for the 'lit' test runner in Android libbcc
# This file is common to both host and target side tests 

import os

# Used to determine the absolute path of a tool. If env_var is set, it
# overrides the default behaviour of searching PATH for binary_name
def inferTool(lit, binary_name, env_var, PATH):
    # Determine which tool to use.
    tool = os.getenv(env_var)

    # If the user set the overriding environment variable, use it
    if tool and os.path.isfile(tool):
        return tool

    # Otherwise look in the path.
    tool = lit.util.which(binary_name, PATH)

    if not tool:
        lit.fatal("couldn't find " + binary_name + " program in " + PATH + " \
                  , try setting " + env_var + " in your environment")

    return os.path.abspath(tool)

# Get the base build directory for the android source tree from environment.
config.build_top = os.getenv('ANDROID_BUILD_TOP')

config.base_build_path = os.path.join(config.build_top, 'out', 'host',
  'linux-x86')

# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.ShTest()

# Tool used to verify debugger output against expected output in source
config.filecheck = inferTool(lit, 'FileCheck', 'FILECHECK', \
  os.path.join(config.base_build_path, 'bin'))

# Invokes GDB and captures output
config.test_bcc_debuginfo = inferTool(lit, 'test_bcc_debuginfo.pl', \
  'TEST_JIT_DEBUGINFO', os.path.join(config.build_top, 'frameworks', \
  'compile', 'libbcc', 'tests', 'debuginfo'))

# GDB
config.gdb = inferTool(lit, 'arm-linux-androideabi-gdb', 'GDB',
  config.environment['PATH'])

# GDB python plugin
config.gdb_plugin = inferTool(lit, 'android-commands.py',
  'ANDROID_GDB_PLUGIN', os.path.join(config.build_top, 'frameworks',
    'compile', 'libbcc', 'gdb_plugin'))
config.gdb_plugin_directory = os.path.dirname(config.gdb_plugin)

# Script interpreters that are not python
config.perl = inferTool(lit, 'perl', 'PERL', config.environment['PATH'])
config.sh = inferTool(lit, 'bash', 'BASH', config.environment['PATH'])

# Tools that are specific to running host-side debugger integration tests:
config.clang = inferTool(lit, 'clang', 'CLANG',
  os.path.join(config.base_build_path, 'bin')).replace('\\', '/')
config.bcc_driver = inferTool(lit, 'bcc', 'BCC_DRIVER',
  os.path.join(config.base_build_path, 'obj', 'EXECUTABLES', \
    'bcc_intermediates')).replace('\\', '/')

# Tools that are specific to running target-side debugger integration tests:
config.build_test_apk = inferTool(lit, 'build_test_apk.sh',
  'BUILD_TEST_APK',
  os.path.join(config.build_top, 'frameworks', 'compile', 'libbcc',
    'tests', 'debuginfo'))

#
## Apply common substitutions
#
config.substitutions.append( ('%Test_jit_debuginfo', config.perl \
                                + ' ' + config.test_bcc_debuginfo \
                                + ' ' + config.filecheck + ' ' ) )

#
## Print common configuration
#
if not lit.quiet:
    lit.note('using bash: %r' % config.sh)
    lit.note('using perl: %r' % config.perl)
    lit.note('using verification script: %r' % config.test_bcc_debuginfo)
    lit.note('using FileCheck: %r' % config.filecheck)
    lit.note('using GDB: %r' % config.gdb)
